package cn.zhf.shop.service.impl;

import cn.hutool.json.JSONUtil;
import cn.zhf.shop.entity.Chinapay;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.Orders;
import cn.zhf.shop.entity.Refund;
import cn.zhf.shop.mapper.ChinapayMapper;
import cn.zhf.shop.mapper.OrdersMapper;
import cn.zhf.shop.mapper.RefundMapper;
import cn.zhf.shop.mapper.ShoppingOrderMapper;
import cn.zhf.shop.service.RefundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
@Service
public class RefundServiceImpl implements RefundService {

    @Resource
    private RefundMapper refundMapper;

    @Resource
    private ChinapayMapper chinapayMapper;

    @Resource
    private ShoppingOrderMapper shoppingOrderMapper;

    @Resource
    private OrdersMapper ordersMapper;

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Override
    public Message<?> insert(@Valid Refund r) {
        logger.info("新建退款申请：{}", JSONUtil.toJsonStr(r));
        r.setState(0);
        Orders orders = ordersMapper.selectByPrimaryKey(r.getOrderId());
        orders.setState(2);
        ordersMapper.updateByPrimaryKey(orders);
        return refundMapper.insert(r) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<Refund> selectByOrderId(@NotBlank(message = "OrderId不得为空") String orderId) {
        logger.info("根据订单编号查找退款申请，orderId：{}",orderId);
        Refund refund = refundMapper.selectByOrderId(orderId);
        return refund == null ? Message.fail("订单未找到") : Message.success(null).add(refund);
    }

    @Override
    public Message<List<Refund>> selectAll() {
        logger.info("查找所有退款申请");
        return Message.success(null).add(refundMapper.selectAll());
    }

    @Override
    public Message<List<Refund>> selectByUserId(@NotBlank(message = "userId不得为空") String userId) {
        logger.info("根据userId查找退款申请 {{}}",userId);
        return Message.success(null).add(refundMapper.selectByUserId(userId));
    }

    @Override
    public Message<List<Refund>> selectByState(@NotBlank(message = "state不得为空") Integer state) {
        logger.info("根据申请状态查找申请：{{}}",state);
        return Message.success(null).add(refundMapper.selectByState(state));
    }

    @Override
    public Message<List<Refund>> selectByUserIdAndState(@NotBlank(message = "userId不得为空") String userId, @NotBlank(message = "state不得为空") Integer state) {
        logger.info("根据用户ID与状态查找申请，ID：{{}},STATE:{{}}",userId,state);
        return Message.success(null).add(refundMapper.selectByUserIdAndState(userId, state));
    }

    @Override
    public Message<?> updateState(@NotBlank(message = "orderId不得为空") String orderId, @NotBlank(message = "state不得为空") Integer state) {
        logger.info("更新退款申请状态 {{}}",orderId);
        if(1 == state){
            Chinapay chinapay = chinapayMapper.selectByPrimaryKey(orderId);
            logger.info("走这个{{}}",chinapay.getOriOrderNo());
            return refundMapper.changeRefundState(orderId,state) > 0 ? Message.success(null).add(chinapay) : Message.fail(null);
        }else{
            return refundMapper.changeRefundState(orderId,state) > 0 ? Message.success(null) : Message.fail(null);
        }
    }


}
